package com.stylefeng.guns.modular.customer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.customer.dao.CustomerCompanyDao;
import com.stylefeng.guns.modular.customer.dao.SaleDao;
import com.stylefeng.guns.modular.customer.service.ISaleService;
import com.stylefeng.guns.modular.customer.transfer.SaleXlsM;
import com.stylefeng.guns.modular.system.dao.SaleMapper;
import com.stylefeng.guns.modular.system.model.CustomerCompany;
import com.stylefeng.guns.modular.system.model.Purchase;
import com.stylefeng.guns.modular.system.model.Sale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.ValidationException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售配套 服务实现类
 * </p>
 *
 * @author yinjc123
 * @since 2018-04-03
 */
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale> implements ISaleService {

    @Autowired
    private SaleDao saleDao;
    @Autowired
    private CustomerCompanyDao customerCompanyDao;

    @Override
    public List<Map<String, Object>> selectSalesPage(Page<Sale> page, Integer customerCompanyId, String customerCompanyName) {
        return saleDao.getSalesPage(page, customerCompanyId, customerCompanyName);
    }

    @Transactional
    @Override
    public void insertXlsList(List<SaleXlsM> xlsMList) {
        for (SaleXlsM xlsM : xlsMList) {
            CustomerCompany customerCompany = customerCompanyDao.selectByUnifiedSocialCreditCode(xlsM.getUnifiedSocialCreditCode());
            if (customerCompany == null) {
                throw new ValidationException(xlsM.getCustomerCompanyName()+"不存在,统一社会信用代码为:" + xlsM.getUnifiedSocialCreditCode());
            }
            xlsM.setCustomerCompanyId(customerCompany.getId());
        }
        for (SaleXlsM xlsM : xlsMList) {
            Sale sale = new Sale();
            BeanKit.copyProperties(xlsM, sale);
            sale.setAnnualSupply(strConvertBigDecimal(xlsM.getAnnualSupply()));
            sale.setAccountReceivableBalance(strConvertBigDecimal(xlsM.getAccountReceivableBalance()));
            sale.setCreditBalance(strConvertBigDecimal(xlsM.getCreditBalance()));
            sale.setCashReceipted(strConvertBigDecimal(xlsM.getCashReceipted()));
            sale.setSilverTicketRecivied(strConvertBigDecimal(xlsM.getSilverTicketRecivied()));
            sale.setBusinessTicketRecivied(strConvertBigDecimal(xlsM.getSilverTicketRecivied()));
            sale.setBusinessTicketDiscount(strConvertBigDecimal(xlsM.getBusinessTicketDiscount()));
            if(xlsM.getPeriod()==null||xlsM.getPeriod() == "") {
                  sale.setPeriod(new Integer(0));
            }else{
                  sale.setPeriod(new Integer(xlsM.getPeriod()));
            }


            super.insert(sale);
        }
    }


    private BigDecimal strConvertBigDecimal(String value) {
        if (value == null || value == "") {
            return new BigDecimal(0);
        }
        return new BigDecimal(value);
    }


    @Override
    public void deleteByCustomerCompanyId(Integer customerCompanyId) {
        EntityWrapper<Sale> ew = new EntityWrapper<Sale>();
        ew.eq("customer_company_id", customerCompanyId);
        delete(ew);
    }
}
